$(document).ready(function(){
	SetDateTimeForSearching('dateFrom','dateTo');

	$('.Create_Bet_Link').click(function(){
		var id = $(this).attr('id');
		var eventId = $('#' + id + '_EventId').val();
		$('#frmEvents_EventId').attr('name','eventInfo.eventId');
		$('#frmEvents_EventId').val(eventId);
		$('#frmEvents').attr('action', BET_CREATE_URL);
		$('#frmEvents').submit();
	});
	
	$('.View_Bets_Link').click(function(){
		var id = $(this).attr('id');
		var eventId = $('#' + id + '_EventId').val();
//		$('#frmEvents_EventId').attr('name','betInfo.eventId');
//		$('#frmEvents_EventId').val(eventId);
//		$('#frmEvents').attr('action', VIEW_BETS_URL);
//		$('#frmEvents').submit();
		
		//$('#frmEvents_Bets_Column').toggle();
		if ($('#frmEvents_Bets_Div').css('display') != 'none')
			showBets(eventId);
		
		//return false;
	});
	
	$('.View_Event_Detail_Link').click(function(){
		var id = $(this).attr('id');
		var eventId = $('#' + id + '_EventId').val();
		var eventDesc = $('#' + id + '_Description').val();
		if ($('#' + id + '_Detail_Data').html().length == 0)
			showPossibilities(eventId, eventDesc);
		
		$('#' + id + '_Detail_Data').toggle();
//		if ($('#' + id + '_Detail_Data').css('display') == 'none')
//			$(this).html('Detail');
//		else $(this).html('Collapse');
		
		return false;
	});
});

function showBets(eventId){
	console.log('eventId:' + eventId);
	var bets_column = $('#frmEvents_Bets_Column');
	var bets_div = $('#frmEvents_Bets_Div');
	$.ajax({
		url : BET_VIEW_LIST_FOR_EVENT_URL + "?betInfo.eventId=" + eventId
		,success : function(data) {
			console.log(data);
			bets_div.html(data);
		}
	});
}


function showPossibilities(eventId, desc){
	$.ajax({
		url : EVENT_VIEW_DETAIL_URL + "?eventInfo.eventId=" + eventId
		,success : function(data) {
			var possibilities = data.eventInfo.posibilityInfoList;

			addDescription(eventId,desc);
			for (index in possibilities){
				addPossibility(data.eventInfo.eventId
					,possibilities[index].posibilityName,possibilities[index].rightFlg);
			}
		}
	});
}

function addDescription(eventId,desc,result){
	$('#frmEvents_'+eventId+'_Detail_Data')
		.append(desc + '<hr>');
}

function addPossibility(eventId,desc,result){
	if (result == null)
		$('#frmEvents_'+eventId+'_Detail_Data')
			.append('* ' + desc + '<br>');
	else $('#frmEvents_'+eventId+'_Detail_Data')
			.append('* ' + desc + ' - ' + getPossibilityResult(result) + '<br>');
}